/**
 * Created by Administrator on 2017/8/17.
 */


var errTitle = '异常';
var warTitle = '错误';
var errMsg = '服务器异常，请联系管理员';
var imgTitle = '预览图片';


$(function () {
    formAjax($('form'));
});
/**
 * ajax提交表单
 * @param formObj
 */
function formAjax(formObj) {
    var count = formObj.length;
    for (var i = 0; i < count; i++) {
        var formItem = formObj.eq(i);
        formItem.Validform({
            tiptype: function (msg, o, cssctl) {
                if (o.type == 3) {
                    layer.msg(msg, function(){});
                }
            },
            callback: function (form) {
                var layars = layer.load(0, {shade: [0.8, '#fff']});
                formItem.ajaxSubmit({
                    url: formItem.attr('action'),
                    type: 'post',
                    enctype: 'multipart/form-data',
                    success: function (data) {
                        layer.close(layars);
                        runAjaxResSuccess(data);
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        layer.close(layars);
                        runAjaxResError(XMLHttpRequest, textStatus, errorThrown);
                    }
                });
                return false;
            }
        });
    }
}
/**
 * 解析ajax请求成功时返回的数据
 * @param data
 */
function runAjaxResSuccess(data) {

    if (data.code === 1) {
        layer.msg(data.msg, {
            icon: 1,
            time: 1000,
            shade: [0.8, '#fff']
        }, function (index) {
            //如果页面是在弹窗
            var indexbox = parent.layer.getFrameIndex(window.name);
            if (indexbox) {
                //关闭弹窗
                if (data.url) {
                    //goUrl(data.url, data.url.indexOf("#this") ? this : parent);
                    if (data.url.indexOf("#this") > 0) {
                        goUrl(data.url, window);
                    } else {
                        goUrl(data.url, parent);
                    }
                    return;
                } else {
                    parent.location.reload();
                }
                parent.layer.close(indexbox);
            }
            layer.close(index);
            if (data.url) {
                goUrl(data.url);
            }
        });
    } else if (data.code === 0) {
        layer.msg(data.msg, {
            icon: 5,
            time: 1000//2秒关闭（如果不配置，默认是3秒）
            // shade: [0.8, '#fff']
        }, function(){
               $('.verify').attr("src", "http://192.168.1.188/pratice/public/admin/index/getcaptcha.html");
        });
        // layer.alert(data.info, {
        //     title: warTitle,
        //     icon: 2
        // }, function (index) {
        //     //如果页面是在弹窗
        //     var indexbox = parent.layer.getFrameIndex(window.name);
        //     if (indexbox) {
        //         //关闭弹窗
        //         if (data.url) {
        //             goUrl(data.url, parent);
        //         }
        //         parent.layer.close(indexbox);
        //     }
        //     layer.close(index);
        //     if (data.url) {
        //         goUrl(data.url);
        //     }
        // });
    } else {
        layer.alert(errMsg, {
            icon: 0,
            title: errTitle
        });
    }
}
/**
 * 跳转url
 * @param url
 * @param obj
 */
function goUrl(url, obj) {
    obj = obj ? obj : self;
    obj.location = url;
}


